var app = getApp()
var util = require('../../utils/util.js');

Page({
  data: {
    userInfo: {},
    isShow: false,
    pageNumber: 1,
    pageSize: 10,
    couponsParam: '',
    trafficType: 0,
    state: 0,
    tabActive: '',
    couponList: [],
    useGoodsTypeArr: { 'all': '全部产品', 'appoint': '指定产品' }
  },
  onLoad: function (options) {
    //调用应用实例的方法获取全局数据
    var that = this
    that.setData({ imgurl: app.imageUrl })
    util.setColor(that); // 配色
    wx.showLoading({
      title: '加载中',
    })
    that.setData({ tabActive: app.colorName + "_tab_active" });
    that.getUserInfo();
    var urlStr = options.params
    urlStr = unescape(urlStr);
    var params = JSON.parse(urlStr)
    that.setData({ couponsParam: params})
    that.loadData()// 初始化查询
  },
  getUserInfo: function () {
    var that = this
    wx.getUserInfo({
      lang: 'zh_CN',
      success: function (res) {
        app.userInfo = res.userInfo
      }
    })
  },
  changeTrafficType: function (event) {
    this.setData({ trafficType: event.currentTarget.dataset.tp });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.setData({ showNoneRefresh: false })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {
    this.setData({ pageNum: 0, list: [], total: 0, showNoneRefresh: false })
    this.loadData()
  },
  onReachBottom: function () {
    if (this.data.list.length < this.data.total) {
      this.loadData()
    } else {
      this.setData({ showNoneRefresh: true })
    }
  },
  // 查询优惠券列表
  loadData() {
    var that = this
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var num = that.data.pageNumber
    var params = that.data.couponsParam
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: app.url + '/cx/retail/getCouponsNum.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        var templist = that.data.list
        if (rest.isSuccess == 0) {
          var list = rest.list;
          for (var i = 0; i < list.length; i++) {
            list[i].startDate = that.dateFun(list[i].startDate);
            list[i].endDate = that.dateFun(list[i].endDate);
          }
          that.setData({ couponList: list })
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
        wx.hideLoading()
        that.setData({ isShow: true })
      }
    })
  },
  dateFun: function (date) {
    var date = new Date(date);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return year + '-' + month + '-' + day;
  },
  // 优惠券状态点击事件
  tabClick: function (e) {
    var tab = e.currentTarget.dataset.tab;
    this.setData({ state: tab, pageSize: 10, pageNumber: 1 });
    this.setData({ tabActive: app.colorName + "_tab_active" });
    this.loadData();
  },
  // 立即使用
  immediateUse: function (e) {
    var item = e.currentTarget.dataset.obj;
    // 将选择优惠券存入缓存
    wx.removeStorageSync("isUseCoupon")
    wx.removeStorageSync("couponObj")
    wx.setStorageSync('isUseCoupon', 'unuse')
    wx.setStorageSync('couponObj', item)

    wx.navigateBack()//返回
  },
  // 不使用
  bingNoUse: function (){
    wx.navigateBack()//返回
  }
})
